<template>
  <div style="display: inline-block;margin-right: 5px;">
    <Modal v-model="modal1"
           title="赠送/索回积分"
           ok-text="提交"
           :loading="loading"
           @on-ok="handleSubmit('formValidate')">
      <Form ref="formValidate"
            class="form"
            :model="formValidate"
            :rules="ruleValidate"
            :label-width="80">
        <FormItem label="赠送/索回"
                  class="form-item"
                  prop="Integral">
          <Select v-model="formValidate.Integral">
            <Option v-for="item in IntegralVariationType"
                    :value="item.number"
                    :key="item.number">{{ item.text }}</Option>
          </Select>
        </FormItem>
        <FormItem label="积分数量"
                  class="form-item"
                  prop="IntegralVariation">
          <InputNumber :min="0"
                       v-model="formValidate.IntegralVariation"></InputNumber>
        </FormItem>
        <FormItem label="积分类型"
                  class="form-item"
                  prop="IntegralType">
          <Select v-model="formValidate.IntegralType">
            <Option v-for="item in IntegralType"
                    :value="item.number"
                    :key="item.number">{{ item.text }}</Option>
          </Select>
        </FormItem>
      </Form>
    </Modal>
  </div>
</template>

<script>
import { editUserintegral } from '@/api/user'

export default {
  name: 'edit',
  data () {
    return {
      modal1: false,
      loading: true,
      IntegralVariationType: [
        {
          number: 0,
          text: '赠送'
        },
        {
          number: 1,
          text: '索回'
        }
      ],
      IntegralType: [
        {
          number: 0,
          text: '收入'
        },
        {
          number: 1,
          text: '支出'
        }
      ],
      formValidate: {},
      ruleValidate: {
        Integral: [
          { required: true, type: 'number', message: '请选择赠送/索回', trigger: 'change' }
        ],
        IntegralVariation: [
          { required: true, type: 'number', message: '请输入积分数量', trigger: 'change' }
        ],
        IntegralType: [
          { required: true, type: 'number', message: '请选择积分类型', trigger: 'change' }
        ]
      },
      currentColId: ''
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    init () {
      this.modal1 = false
      this.loading = true
      this.formValidate = {}
      this.editData = {}
      this.handleReset('formValidate')
    },
    getItem (id) {
      this.handleReset('formValidate')
      this.currentColId = id
      this.modal1 = true
    },
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          if (this.Integral === 0) {
            this.formValidate.IntegralVariation = '+' + this.formValidate.IntegralVariation
          } else {
            this.formValidate.IntegralVariation = '-' + this.formValidate.IntegralVariation
          }
          this.formValidate.SysUserId = localStorage.getItem('userId')
          this.formValidate.UserID = this.currentColId
          editUserintegral(this.formValidate).then(res => {
            if (res.data.success) {
              this.modal1 = false
              this.loading = true
              this.$Message.info('编辑成功!')
            } else {
              this.modal1 = true
              this.loading = false
              this.$Message.info('编辑失败!')
            }
          }).then(() => {
            this.$emit('getList')
          })
        } else {
          this.modal1 = true
          this.loading = false
          this.$Message.info('请完善填写信息！')
        }
      })
    },
    // 重置表单
    handleReset (name) {
      this.$refs[name].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
